package vn.edu.saigontech.ncms.web.bookstore;

import java.util.Collection;
import java.util.List;

import vn.edu.saigontech.ncms.model.bookstore.ItemTitle;
import vn.edu.saigontech.ncms.model.program.Level;
import vn.edu.saigontech.ncms.model.program.Program;
import vn.edu.saigontech.ncms.service.ItemTitleService;
import vn.edu.saigontech.ncms.service.LevelService;
import vn.edu.saigontech.ncms.service.ProgramService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class TitleAction extends BaseAction {
	private Long id;
	private String title;
	private String comment;
	private boolean active;
	private Long[] levelIds;
	private Long programId;

	private ItemTitle itemTitle;
	private Program program;
	private List<ItemTitle> titles;
	private Collection<Program> programs;

	/**
	 * show add title
	 * 
	 * @return
	 */
	public String showAdd() {
		this.program = ProgramService.INSTANCE.findById(this.programId);
		this.programs = ProgramService.INSTANCE.findAll();
		return SUCCESS;
	}

	/**
	 * add title
	 * 
	 * @return
	 */
	public String add() {
		ItemTitle itemTitle = new ItemTitle();
		itemTitle.setTitle(this.title);
		itemTitle.setComment(this.comment);
		itemTitle.setActive(true);
		this.program = ProgramService.INSTANCE.findById(this.programId);
		itemTitle.setProgram(this.program);
		for(Long each : this.levelIds) {
			Level level = LevelService.INSTANCE.findById(each);
			itemTitle.addLevel(level);
		}
		itemTitle = ItemTitleService.INSTANCE.persist(itemTitle);
		this.addLoggingDetail(BaseAction.ADD, "logging.title.add", itemTitle
				.getTitle(), program.getName());
		return SUCCESS;
	}

	/**
	 * list titles
	 * @return
	 */
	public String list() {
		this.program = ProgramService.INSTANCE.findById(this.programId);
		if (Checker.isNull(program)) {
			this.program = ProgramService.INSTANCE.getFirst();
		}
		this.programs = ProgramService.INSTANCE.findAll();
		this.titles = ItemTitleService.INSTANCE.findByProgram(this.program);
		return SUCCESS;
	}

	public String showEdit() {
		itemTitle = ItemTitleService.INSTANCE.findById(this.id);
		this.programs = ProgramService.INSTANCE.findAll();
		return SUCCESS;
	}

	public String edit() {
		ItemTitle itemTitle = ItemTitleService.INSTANCE.findById(this.id);
		itemTitle.setTitle(this.title);
		itemTitle.setComment(this.comment);
		itemTitle.setActive(this.active);
		this.program = ProgramService.INSTANCE.findById(this.programId);
		itemTitle.setProgram(this.program);
		itemTitle.resetLevels();
		for(Long each : this.levelIds) {
			Level level = LevelService.INSTANCE.findById(each);
			itemTitle.addLevel(level);
		}
		itemTitle = ItemTitleService.INSTANCE.persist(itemTitle);
		if (Checker.isNotNull(itemTitle)) {
			this.addLoggingDetail(BaseAction.EDIT, "logging.title.edit", itemTitle.getTitle(), this.program.getName());
			return SUCCESS;
		}
		return ERROR;
	}

	public String delete() {
		ItemTitle title = ItemTitleService.INSTANCE.findById(this.id);
		String name = title.getTitle();
		if (ItemTitleService.INSTANCE.deleteById(this.id)) {
			this.setKeyMessage("confirm.deleteSuccessfully", "bookstore.title");
			this.addLoggingDetail(BaseAction.DELETE, "logging.title.delete", name);
		} else 
			this.setKeyMessage("confirm.deleteUnsuccessfully", "bookstore.title");
		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Long getProgramId() {
		return programId;
	}

	public void setProgramId(Long programId) {
		this.programId = programId;
	}

	public Program getProgram() {
		return program;
	}

	public void setProgram(Program program) {
		this.program = program;
	}

	public List<ItemTitle> getTitles() {
		return titles;
	}

	public void setTitles(List<ItemTitle> titles) {
		this.titles = titles;
	}

	public Collection<Program> getPrograms() {
		return programs;
	}

	public void setPrograms(Collection<Program> programs) {
		this.programs = programs;
	}

	public ItemTitle getItemTitle() {
		return itemTitle;
	}

	public void setItemTitle(ItemTitle itemTitle) {
		this.itemTitle = itemTitle;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public Long[] getLevelIds() {
		return levelIds;
	}

	public void setLevelIds(Long[] levelIds) {
		this.levelIds = levelIds;
	}
}
